﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;
using System.Data.SqlClient;
using ELS.User;
using Microsoft.SqlServer.Management.Sdk.Sfc;

namespace ELS.BackupRestore
{
    public partial class RestoreForm : MdiChildForm
    {
        

        public RestoreForm()
        {
            InitializeComponent();
        }

        BackupRestoreController restoreController = new BackupRestoreController();
        private TextBox textBox;
        private PictureBox pictureBox;
        private Panel pnlMainForm;
        public RestoreForm(Panel pnlMainForm)
        {   
            this.pnlMainForm = pnlMainForm;
            InitializeComponent();
            LoadListFileData();
        }

        public RestoreForm(TextBox txtStatus, PictureBox pbStatus, Panel pnlMainForm)
        {
            InitializeComponent();
            LoadListFileData();
            this.textBox = txtStatus;
            this.pictureBox = pbStatus;
            this.pnlMainForm = pnlMainForm;
        }
        private void LoadListFileData()
        {
            drgRestoreList.AutoGenerateColumns = false;
            drgRestoreList.DataSource = restoreController.GetListFileData();
        }
        
        public void Backup(string FileName, List<string> Tables)
        {
            try
            {
                SqlConnection connect = new SqlConnection(UserCommon.connectionString);
                StringBuilder sb = new StringBuilder();
                Server srv = new Server(new Microsoft.SqlServer.Management.Common.ServerConnection(connect));
                Database dbs = srv.Databases["ELS_v4"];

                List<Urn> list = new List<Urn>();
                DataTable dataTable = dbs.EnumObjects(DatabaseObjectTypes.Table);
                foreach (Table tb in dbs.Tables)
                {
                    if (tb.Name != "FileSave")
                    {
                        list.Add(new Urn((string)tb.Urn));
                    }
                }

                Scripter scripter = new Scripter();
                scripter.Server = srv;
                scripter.Options.ScriptData = true;
                scripter.Options.ScriptDrops = false;
                scripter.Options.ScriptSchema = true;
                scripter.Options.FileName = @"D:\tables.sql";
                scripter.Options.EnforceScriptingOptions = true;
                scripter.Options.WithDependencies = true;
                scripter.Options.Indexes = true;
                scripter.Options.AppendToFile = true;
                scripter.Options.IncludeHeaders = true;

                scripter.Options.ClusteredIndexes  = true;
                scripter.Options.NonClusteredIndexes = true;
                scripter.Options.Default  = true;
                scripter.Options.FullTextIndexes = true;        
                scripter.Options.SchemaQualify  = true;
                scripter.Options.Statistics  = true;
                scripter.Options.Triggers  = true;
                scripter.Options.DriAll   = true;

                scripter.EnumScript(list.ToArray());
            }
            catch (NullReferenceException e)
            {
                MessageBox.Show(e.Message +"\n"+ e.StackTrace);
            }
         }
        int currentFileId = -1;
        private void btnRestore_Click(object sender, EventArgs e)
        {
            if (currentFileId >= 0)
            {
                DialogResult dialogConfirm = MessageBox.Show(UserCommon.RestoreFile_Message, UserCommon.RestoreFile_MessageBox_Title, MessageBoxButtons.YesNo);
                if (dialogConfirm == DialogResult.Yes)
                {
                    bool isRestoreSuccess = restoreController.RestoreDb(currentFileId);
                    if (isRestoreSuccess)
                    {
                        // reload content
                        // LoadListFileData();
                        UserCommon.NotifyOnMainForm(textBox, pictureBox, UserCommon.RestoreFile_Success, UserCommon.DONE_STATUS);                        
                    }
                    else
                    {
                        UserCommon.NotifyOnMainForm(textBox, pictureBox, UserCommon.RestoreFile_Fail, UserCommon.FAIL_STATUS);
                    }
                }
            }
            else
            {
                UserCommon.NotifyOnMainForm(textBox, pictureBox, UserCommon.RestoreFile_DoNotSelect, UserCommon.WARNING_STATUS);
            }
        }

        private void drgRestoreList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            currentFileId = int.Parse(drgRestoreList[0, e.RowIndex].FormattedValue.ToString());
        }
       
    }
}
